//// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
//package com.linqingying.gitcode.pullrequest.ui.details.model.impl
//
//import com.intellij.collaboration.async.cancelAndJoinSilently
//import com.intellij.collaboration.async.nestedDisposable
//import com.intellij.collaboration.ui.codereview.details.data.ReviewRequestState
//import com.intellij.collaboration.ui.codereview.details.model.CodeReviewDetailsViewModel
//import com.intellij.collaboration.ui.codereview.issues.processIssueIdsHtml
//import com.intellij.collaboration.ui.icon.IconsProvider
//import com.intellij.openapi.project.Project
//import com.intellij.platform.util.coroutines.childScope
//import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.flow.*
//import org.jetbrains.annotations.ApiStatus
//import com.linqingying.gitcode.api.data.GCCommit
//import com.linqingying.gitcode.api.data.pullrequest.GCPullRequest
//import com.linqingying.gitcode.api.data.pullrequest.GCPullRequestState
//import com.linqingying.gitcode.pullrequest.comment.convertToHtml
//import com.linqingying.gitcode.pullrequest.data.GCPRDataContext
//import com.linqingying.gitcode.pullrequest.data.provider.GCPRDataProvider
//import com.linqingying.gitcode.pullrequest.data.service.GCPRSecurityService
//import com.linqingying.gitcode.pullrequest.ui.GCCompletableFutureLoadingModel
//import com.linqingying.gitcode.pullrequest.ui.details.model.GCPRBranchesViewModel
//import com.linqingying.gitcode.pullrequest.ui.details.model.GCPRStatusViewModelImpl
//import com.linqingying.gitcode.pullrequest.ui.review.GCPRReviewViewModelHelper
//
//@ApiStatus.Experimental
//interface GCPRDetailsViewModel : CodeReviewDetailsViewModel {
//  val securityService: GCPRSecurityService
//  val avatarIconsProvider: IconsProvider<String>
//
//  val isUpdating: StateFlow<Boolean>
//
//  val branchesVm: GCPRBranchesViewModel
//  val changesVm: GCPRChangesViewModel
//  val statusVm: GCPRStatusViewModelImpl
//  val reviewFlowVm: GCPRReviewFlowViewModelImpl
//}
//
//internal class GCPRDetailsViewModelImpl(
//  project: Project,
//  parentCs: CoroutineScope,
//  dataContext: GCPRDataContext,
//  dataProvider: GCPRDataProvider,
//  details: GCPullRequest
//) : GCPRDetailsViewModel {
//  private val cs = parentCs.childScope()
//
//  private val detailsState = MutableStateFlow(details)
//
//  override val number: String = "#${detailsState.value.number}"
//  override val url: String = detailsState.value.url
//
//  override val title: SharedFlow<String> = detailsState.map { it.title }
//    .map { title -> title.convertToHtml(project) }
//    .shareIn(cs, SharingStarted.Lazily, 1)
//
//  override val description: SharedFlow<String> = detailsState.map { it.body }
//    .map { description -> processIssueIdsHtml(project, description) }
//    .shareIn(cs, SharingStarted.Lazily, 1)
//
//  override val reviewRequestState: SharedFlow<ReviewRequestState> = detailsState.map { details ->
//    if (details.isDraft) return@map ReviewRequestState.DRAFT
//    when (details.state) {
//      GCPullRequestState.CLOSED -> ReviewRequestState.CLOSED
//      GCPullRequestState.MERGED -> ReviewRequestState.MERGED
//      GCPullRequestState.OPEN -> ReviewRequestState.OPENED
//    }
//  }.shareIn(cs, SharingStarted.Lazily, 1)
//
//  private val commitsLoadingModel = GCCompletableFutureLoadingModel<List<GCCommit>>(cs.nestedDisposable())
//
//  init {
//    dataProvider.changesData.loadCommitsFromApi(cs.nestedDisposable()) {
//      commitsLoadingModel.future = it
//    }
//  }
//
//  override val isUpdating = MutableStateFlow(false)
//
//  override val securityService: GCPRSecurityService = dataContext.securityService
//  override val avatarIconsProvider: IconsProvider<String> = dataContext.avatarIconsProvider
//  override val branchesVm = GCPRBranchesViewModel(cs, project, dataContext.repositoryDataService.repositoryMapping, detailsState)
//
//  private val reviewVmHelper = GCPRReviewViewModelHelper(cs, dataProvider)
//  override val changesVm = GCPRChangesViewModelImpl(cs, project, dataContext, dataProvider)
//
//  override val statusVm = GCPRStatusViewModelImpl(cs, project, detailsState, dataProvider.stateData)
//
//  override val reviewFlowVm =
//    GCPRReviewFlowViewModelImpl(cs,
//                                project,
//                                detailsState,
//                                dataContext.repositoryDataService,
//                                dataContext.securityService,
//                                dataContext.avatarIconsProvider,
//                                dataProvider.detailsData,
//                                dataProvider.stateData,
//                                dataProvider.changesData,
//                                reviewVmHelper)
//
//  fun update(details: GCPullRequest) {
//    detailsState.value = details
//  }
//
//  suspend fun destroy() = cs.cancelAndJoinSilently()
//}
